package servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.Admin;
import entity.Student;
import service.AdminService;
import service.StudentService;
import service.serviceImpl.AdminServiceimpl;
import service.serviceImpl.StudentServiceimpl;

/**
 * Servlet implementation class login_servlet
 */
@WebServlet("/AdminLogin")
public class AdminLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public AdminLogin() {
        super();

    }

	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		req.setCharacterEncoding("utf-8");
		
		String name = req.getParameter("name");
		String password =req.getParameter("password");
		String login=req.getParameter("login");
		
		if(name==null||password==null ) {
			req.getRequestDispatcher("/login.jsp").forward(req, resp);
			return;
		}
		if("admin".equals(login)) {  
			
			AdminService admins=new AdminServiceimpl();
			Admin admin=admins.adminLogin(name, password);
			
			if(admin!=null&&admin.getName()!=null) {
				//閲嶅畾鍚�
				resp.sendRedirect("admin.jsp");
			}else {
				req.setAttribute("msg", "账号/密码有问题");
				req.getRequestDispatcher("/login.jsp").forward(req, resp);
				}
			}
		else {       
			    Student stu=new Student();
			    stu.setSno(Integer.parseInt(name));
			    stu.setPassword((Integer.parseInt(password)));
			    StudentService stus=new StudentServiceimpl();
			  
				Student stu1=stus.stuLogin(stu);
			
				 System.out.println(stu1.toString()); 
				if(stu1!=null&&stu1.getSno()!=0) {
					
					req.setAttribute("studentId",stu1.getSno() );
					req.getRequestDispatcher("/student.jsp").forward(req, resp);
				}else {
					req.setAttribute("msg", "账号/密码有问题");
					req.getRequestDispatcher("/login.jsp").forward(req, resp);
				}
			} 
	}
}
